﻿using System.Web.Mvc;
using System.Web.Routing;
using CRM.Database.Model;

namespace CRM.Controllers
{
    [CheckAccess]
    public class CustomersManagementController : Controller
    {
        //
        // GET: /CustomersManagement/

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult CustomersMerging()
        {
            //var col = new CustomersCollection();
            //col.CustomersMerging(9299, 8767);
            return View();
        }


        public ActionResult CustomerMergingSuccess()
        {
            return View();
        }

        public ActionResult CustomersMergingError()
        {
            return View();
        }
        
        [HttpPost]
        public ActionResult CustomersMerging(object obj)
        {
            int firstCust, secondCust;

            int.TryParse(Request["firstCustomerNumber"], out firstCust);
            int.TryParse(Request["secondCustomerNumber"], out secondCust);

            if(firstCust>0 & secondCust>0)
            {
                var col = new CustomersCollection();
                if( col.CustomersMerging(firstCust, secondCust))
                return RedirectToAction("CustomerMergingSuccess");
                else
                {
                    return RedirectToAction("CustomersMergingError");
                }
            }

            else
            {
                return RedirectToAction("CustomersMergingError");
            }
                
            
           
          
        }


        [HttpGet]
        public ActionResult CutomersDeleting()
        {
            return View();
        }


        [HttpPost]
        public ActionResult  CutomersDeleting(object c)
        {
            int custNumber = 0;
            int.TryParse(Request.Params["customerNumber"], out custNumber);

            if(custNumber>0)
            {
            //    var custCol = new CustomersCollection();
            //    //return View("CustomersDeletingConfirmation",custCol.GetCustomerBySequentalNumber(custNumber));
            //    // return RedirectToAction("Details", new RouteValueDictionary(new { id = idCust }));
                return RedirectToAction("CustomersDeletingConfirmation",
                                 new RouteValueDictionary(new { num = custNumber }));
            }
            else
            {
        return        RedirectToAction("WrongNumber",
                                 new RouteValueDictionary(new { num = custNumber }));
            }
            //{return RedirectToAction("Error", "Home");}

        
           // return View();

        }


        public ActionResult WrongNumber(int num)
        {
            ViewBag.Message = "Клиента с номером "+num+" нет";
            return View();
        }


        [HttpGet]
        public ActionResult CustomersDeletingConfirmation(int num)
        {

            if (num > 0)
            {
                var custCol = new CustomersCollection();
                return View(custCol.GetCustomerBySequentalNumber(num));
            }


            return View();
        }

        [HttpPost]
        public ActionResult CustomersDeletingConfirmation()
        {
            int custNum = 0;
            int.TryParse(Request["num"], out custNum);
            return RedirectToAction("DeleteCustomer", new RouteValueDictionary(new { custNum = custNum}));
        }

        public ActionResult DeleteCustomer(int custNum)
        {
            var custCol = new CustomersCollection();

           if( custCol.Delete(custCol.GetCustomerBySequentalNumber(custNum)))

            return View();
            return RedirectToAction("Error", "Home");
        }


       

    }
}
